package makabakaquestionnaire.zutsoftware.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import makabakaquestionnaire.zutsoftware.constant.Result;
import makabakaquestionnaire.zutsoftware.entity.ProjectSetting;
import makabakaquestionnaire.zutsoftware.service.impl.ProjectSettingServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "问卷设置接口")
@RestController
@RequestMapping("project/setting")
public class ProjectSettingController {
    ProjectSettingServiceImpl projectSettingService;

    @Autowired
    public void setProjectSettingService(ProjectSettingServiceImpl projectSettingService) {
        this.projectSettingService = projectSettingService;
    }

    @GetMapping("/{ProKey}")
    public Result querySettingByProKey(@PathVariable("ProKey") String ProKey) {
        ProjectSetting projectSetting = projectSettingService.querySettingByProKey(ProKey);
        return Result.success(projectSetting);
    }

    @PostMapping("/save")
    public Result saveOrUpdateSetting(@RequestBody ProjectSetting projectSetting) {
        ProjectSetting oldSetting = projectSettingService.querySettingByProKey(projectSetting.getProjectKey());
        if (ObjectUtil.isNotEmpty(oldSetting)) {
            boolean update = projectSettingService.updateProjectSetting(projectSetting);
            return Result.success(projectSetting, "问卷设置更新" + update);
        } else {
            boolean create = projectSettingService.saveProjectSetting(projectSetting);
            return Result.success(projectSetting, "问卷设置创建" + create);
        }
    }

}
